<script>
	import baseApi from '@/api/baseApi.js'
	export default {
		onLaunch: function() {
			console.log('App Launch')
			this.getRegions()
			const token = uni.getStorageSync('minshehongToken')
			if(token){
				this.getInfo()
			}
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods:{
			getInfo() {
				baseApi.info().then(res => {
					this.$store.commit("setUser", res.data)
					uni.setStorageSync("region_id", res.data.region_id);
					const curRegion = uni.getStorageSync('curMinRegion')
					if(curRegion){
							this.$store.commit("setCurRegion", curRegion)
					} else {
						this.$store.commit("setCurRegion", res.data.regions)
					}
				})
			},
			getRegions(){
				baseApi.regions().then(res => {
					const regions = res.data || []
					regions.forEach(i => {
						i.text = i.name
						i.value = i.id.toString()
						if(i.children){
							i.children.forEach(j => {
								j.text = j.name
								j.value = j.id.toString()
								if(j.children){
									j.children.forEach(k => {
										k.text = k.name
										k.value = k.id.toString()
									})
									const index = j.children.findIndex(item => item.text === '全部')
									if(index < 0 ){
										if(j.children.length){
											j.children.unshift({
												text:'全部',
												value:j.id + '-0'
											})
										}
									}
								}
							})
							const index = i.children.findIndex(item => item.text === '全部')
							if(index <0 ){
								if(i.children.length){
									i.children.unshift({
										text:'全部',
										value:i.id + '-0'
									})
								}
							}
						}
					})
					this.$store.commit("setRegions", regions)
				})
			}
		}
	}
</script>

<style>
	/*每个页面公共css */
	@import url("/common/uni.css");
	@import url("./static/font/iconfont.css");
	@import url("/common/free.css");
	@import url("/common/common.css");
	@import url("/common/color.css");
	@import url("/common/public.css");
</style>
